package com.jwgf.smarowindingmachine_unity.logic;

import android.graphics.Color;

import com.jwgf.Half485.modbusthreadpool.PLCData;
import com.jwgf.common.AppContext;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.ui.SmaroWindingMachineActivity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.util.ChartUtils;

public class DirectCurrentStatus {
    private static DirectCurrentStatus instance = new DirectCurrentStatus();
    private final int MAX_POINTS = 60;
    private LinkedList<Integer> voltageList;
    private LinkedList<Integer> currentList;
    private LinkedList<Integer> temperatureList;

    private int recentVoltage;
    private int recentCurrent;
    private int recentTemperature;

    public static DirectCurrentStatus getInstance() {
        return instance;
    }

    private DirectCurrentStatus() {
        voltageList = new LinkedList<>();
        currentList = new LinkedList<>();
        temperatureList = new LinkedList<>();
        for (int i = 0; i < MAX_POINTS; ++i) {
            voltageList.add(0);
            currentList.add(0);
            temperatureList.add(0);
        }
        Disposable disposable = Observable.interval(1, TimeUnit.MINUTES).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                if (AppContext.isDebug) {
                    int voltage = (int) (Math.random() * 300);
                    int current = (int) (Math.random() * 20);
                    int temperature = (int) (Math.random() * 60);
                    add(voltage, current, temperature);
                } else {
                    int voltage = PLCData.getInstance().getHoldingRegister(1, 245);
                    int current = PLCData.getInstance().getHoldingRegister(1, 246);
                    int temperature = PLCData.getInstance().getHoldingRegister(1, 247);
                    add(voltage, current, temperature);
                }

            }
        });
    }


    public String getRecentVoltage() {
        String string = AppContext.getInstance().getString(R.string.recent_value);
        return string + ":" + recentVoltage + "V";
    }

    public String getRecentCurrent() {
        String string = AppContext.getInstance().getString(R.string.recent_value);
        return string + ":" + recentCurrent + "A";
    }

    public String getRecentTemperature() {
        String string = AppContext.getInstance().getString(R.string.recent_value);
        return string + ":" + recentTemperature + "℃";
    }

    public LineChartData getVoltageData() {
        LinkedList<Integer> dataList = new LinkedList<>((Collection<? extends Integer>) voltageList.clone());
        LineChartData lineChartData = new LineChartData();
        List<Line> lines = new ArrayList<>();
        List<PointValue> points = new ArrayList<>();
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 0; i < dataList.size(); ++i) {
            points.add(new PointValue(i, dataList.get(i)));
            axisValues.add(new AxisValue(i).setLabel(String.valueOf(dataList.size() - i)));
        }
        Line line = new Line(points);
        line.setColor(ChartUtils.COLOR_BLUE);
        line.setShape(ValueShape.CIRCLE);
        line.setCubic(false);
        line.setFilled(false);
        line.setHasLabels(false);
        line.setHasLabelsOnlyForSelected(true);
        line.setHasLines(true);
        line.setHasPoints(true);

        lines.add(line);
        lineChartData.setLines(lines);

        Axis axisX = new Axis();
        Axis axisY = new Axis().setHasLines(true);
        axisX.setTextColor(Color.BLACK);//设置x轴字体的颜色
        axisY.setTextColor(Color.BLACK);//设置y轴字体的颜色
        axisX.setName(AppContext.getInstance().getResources().getString(R.string.recent_minutes));
        axisY.setName("V");
        axisX.setValues(axisValues);
        lineChartData.setAxisXBottom(axisX);
        lineChartData.setAxisYLeft(axisY);


        return lineChartData;
    }

    public LineChartData getCurrentData() {
        LinkedList<Integer> dataList = new LinkedList<>((Collection<? extends Integer>) currentList.clone());
        LineChartData lineChartData = new LineChartData();
        List<Line> lines = new ArrayList<>();
        List<PointValue> points = new ArrayList<>();
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 0; i < dataList.size(); ++i) {
            points.add(new PointValue(i, dataList.get(i)));
            axisValues.add(new AxisValue(i).setLabel(String.valueOf(dataList.size() - i)));
        }
        Line line = new Line(points);
        line.setColor(ChartUtils.COLOR_BLUE);
        line.setShape(ValueShape.CIRCLE);
        line.setCubic(false);
        line.setFilled(false);
        line.setHasLabels(false);
        line.setHasLabelsOnlyForSelected(true);
        line.setHasLines(true);
        line.setHasPoints(true);

        lines.add(line);
        lineChartData.setLines(lines);

        Axis axisX = new Axis();
        Axis axisY = new Axis().setHasLines(true);
        axisX.setTextColor(Color.BLACK);//设置x轴字体的颜色
        axisY.setTextColor(Color.BLACK);//设置y轴字体的颜色
        axisX.setName(AppContext.getInstance().getResources().getString(R.string.recent_minutes));
        axisY.setName("A");
        axisX.setValues(axisValues);
        lineChartData.setAxisXBottom(axisX);
        lineChartData.setAxisYLeft(axisY);

        return lineChartData;
    }

    public LineChartData getTemperatureData() {
        LinkedList<Integer> dataList = new LinkedList<>((Collection<? extends Integer>) temperatureList.clone());
        LineChartData lineChartData = new LineChartData();
        List<Line> lines = new ArrayList<>();
        List<PointValue> points = new ArrayList<>();
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 0; i < dataList.size(); ++i) {
            points.add(new PointValue(i, dataList.get(i)));
            axisValues.add(new AxisValue(i).setLabel(String.valueOf(dataList.size() - i)));
        }
        Line line = new Line(points);
        line.setColor(ChartUtils.COLOR_BLUE);
        line.setShape(ValueShape.CIRCLE);
        line.setCubic(false);
        line.setFilled(false);
        line.setHasLabels(false);
        line.setHasLabelsOnlyForSelected(true);
        line.setHasLines(true);
        line.setHasPoints(true);

        lines.add(line);
        lineChartData.setLines(lines);

        Axis axisX = new Axis();
        Axis axisY = new Axis().setHasLines(true);
        axisX.setTextColor(Color.BLACK);//设置x轴字体的颜色
        axisY.setTextColor(Color.BLACK);//设置y轴字体的颜色
        axisX.setName(AppContext.getInstance().getResources().getString(R.string.recent_minutes));
        axisY.setName("℃");
        axisX.setValues(axisValues);
        lineChartData.setAxisXBottom(axisX);
        lineChartData.setAxisYLeft(axisY);

        return lineChartData;
    }

    private void add(int voltage, int current, int temperature) {
        recentVoltage = voltage;
        recentCurrent = current;
        recentTemperature = temperature;
        if (voltageList.size() < MAX_POINTS) {
            voltageList.add(voltage);
        } else {
            voltageList.removeFirst();
            voltageList.add(voltage);
        }
        if (currentList.size() < MAX_POINTS) {
            currentList.add(current);
        } else {
            currentList.removeFirst();
            currentList.add(current);
        }
        if (temperatureList.size() < MAX_POINTS) {
            temperatureList.add(temperature);
        } else {
            temperatureList.removeFirst();
            temperatureList.add(temperature);
        }
    }
}
